package com.cgsrp.cgsrp_baby.service.impl;

import com.cgsrp.cgsrp_baby.domain.Vaccine;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import com.cgsrp.cgsrp_baby.dao.ChildParentMapper;
import com.cgsrp.cgsrp_baby.domain.ChildParent;
import com.cgsrp.cgsrp_baby.service.ChildParentService;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

@Service
public class ChildParentServiceImpl implements ChildParentService{

    @Resource
    private ChildParentMapper childParentMapper;

    @Override
    public int deleteByPrimaryKey(Integer childId,Integer parentId) {
        return childParentMapper.deleteByPrimaryKey(childId,parentId);
    }

    @Override
    public int insert(ChildParent record) {
        return childParentMapper.insert(record);
    }

    @Override
    public int insertSelective(ChildParent record) {
        return childParentMapper.insertSelective(record);
    }

    @Override
    public ChildParent selectByPrimaryKey(Integer childId,Integer parentId) {
        return childParentMapper.selectByPrimaryKey(childId,parentId);
    }

    @Override
    public int updateByPrimaryKeySelective(ChildParent record) {
        return childParentMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(ChildParent record) {
        return childParentMapper.updateByPrimaryKey(record);
    }

    @Override
    public PageInfo<ChildParent> list(int[] babyIds, Integer curPage, Integer pageSize) {
        List<ChildParent> list= new ArrayList<>();
        for (int babyId : babyIds) {
            List<ChildParent> list1=childParentMapper.list(babyId);
            list.addAll(list1);
        }
        list.sort(Comparator.comparingInt(ChildParent::getChildId));
        list.sort(Comparator.comparingInt(ChildParent::getParentId));

        List<ChildParent> list1=new ArrayList<>();
        if (pageSize<list.size()-(curPage-1)*pageSize) {
            list1 = list.subList((curPage - 1) * pageSize, (curPage - 1) * pageSize + pageSize);
        }else {
            list1 = list.subList((curPage - 1) * pageSize, list.size());
        }
        PageInfo<ChildParent> pageInfo = new PageInfo<>(list1);
        pageInfo.setTotal(list.size());
        PageHelper.startPage(curPage, pageSize);
        return pageInfo;
    }

}
